package admin

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"duoke/utils/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var Carmine = new(carmineCtl)

type carmineCtl struct{}

func (ctl *carmineCtl) Save(c *gin.Context) {
	var req model.ReqCarmine
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), "")
		return
	}
	if req.IsEffective == 1 && req.ExpirationDate == "" {
		response.Error(c, "参数为空【expiration_date】", "")
		return
	}
	if req.IsEffective == 2 && req.ExpirationDay == 0 {
		response.Error(c, "参数为空【expiration_day】", "")
		return
	}
	err := service.Carmine.Save(c, &req)
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "保存成功", "")
	return
}
func (ctl *carmineCtl) Info(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	if id == 0 {
		response.Error(c, "参数缺失【id】", "")
		return
	}
	info, err := service.Carmine.Info(c, &model.ReqCarmine{Id: id})
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "获取成功", info)
}
func (ctl *carmineCtl) List(c *gin.Context) {
	var req model.SearchCarmine
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	list, err := service.Carmine.List(c, &req)
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "获取成功", list)
}
func (ctl *carmineCtl) Del(c *gin.Context) {
	json := request.JsonMap(c)
	id := gconv.Int(json["id"])
	if id == 0 {
		response.Error(c, "参数缺失【id】", "")
		return
	}
	err := service.Carmine.Del(c, id)
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "删除成功", "")
}

// GeneratedCode 生成码
func (ctl *carmineCtl) GeneratedCode(c *gin.Context) {
	var req model.SearchUserCarmine
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), "")
		return
	}
	req.Types = 1
	err := service.Carmine.GeneratedCode(c, &req)
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "生成成功", "")
}
